<?php
/**
 * @ClassName: Equipmentcustomer
 * @Description: TODO
 * @author: gydtrade
 * @Date: 2025-11-20 14:45
 * @Version: V1.0.0
 */

namespace app\admin\controller;

use app\common\model;
use think\Controller;
use tpext\builder\traits\HasBuilder;

class Equipmentcustomer extends Controller
{
    use HasBuilder;

    protected $dataModel;

    protected function initialize()
    {
        parent::initialize();

        $this->dataModel = new model\EquipmentCustomer();
        $this->pageTitle = '设备绑定客户';
        $this->pagesize = 10;

        $this->indexWith = ['quipment', 'customer'];
    }

    /**
     * 构建搜索
     *
     * @return void
     */
    protected function buildSearch()
    {
        $search = $this->search;

        $search->select('device_type', '设备类型')->dataUrl(url("/admin/equipmenttype/selectPage"));
        $search->select('device_id', '设备')->dataUrl(url("/admin/equipment/selectPage"));
        $search->select('customer_id', '客户')->dataUrl(url("/admin/customer.customer/selectPage"));

        $search->select('province', '省份')->dataUrl(url('api/areacity/province'), 'ext_name')->withNext(
            $search->select('city', '城市')->dataUrl(url('api/areacity/city'), 'ext_name')->withNext(
                $search->select('area', '地区')->dataUrl(url('api/areacity/area'), 'ext_name')
            )
        );
        $search->text('address', '安装地址');
    }

    /**
     * 构建搜索条件
     * @return array
     */
    protected function filterWhere()
    {
        $searchData = request()->get();

        $where = [];
        if (isset($searchData['device_type']) && $searchData['device_type'] != '') {
            $where[] = ['device_type', '=', $searchData['device_type']];
        }
        if (isset($searchData['device_id']) && $searchData['device_id'] != '') {
            $where[] = ['device_id', '=', $searchData['device_id']];
        }
        if (isset($searchData['customer_id']) && $searchData['customer_id'] != '') {
            $where[] = ['customer_id', '=', $searchData['customer_id']];
        }
        if (!empty($searchData['province'])) {
            $where[] = ['province', 'eq', $searchData['province']];
        }
        if (!empty($searchData['city'])) {
            $where[] = ['city', 'eq', $searchData['city']];
        }
        if (!empty($searchData['area'])) {
            $where[] = ['area', 'eq', $searchData['area']];
        }
        if (!empty($searchData['address'])) {
            $where[] = ['address', 'eq', '%' . $searchData['address'] . '%'];
        }

        return $where;
    }

    /**
     * 构建表格
     *
     * @return void
     */
    protected function buildTable(&$data = [])
    {
        $table = $this->table;

        $table->show('id', 'ID');
        $table->show('name', '设备名称');
        $table->show('device_type', '设备类型');
        $table->fields('times_group', '添加/修改时间')->with(
            $table->show('create_time', '安装时间')->default('---'),
            $table->show('update_time', '修改时间')->default('---')
        )->getWrapper()->addStyle('width:160px');

        $table->sortable('id, create_time');

        $table->getToolbar()
            ->btnAdd()
            ->btnRefresh();

        $table->getActionbar()
            ->btnEdit();
    }

    /**
     * 构建表单
     *
     * @param boolean $isEdit
     * @param array $data
     */
    protected function buildForm($isEdit, &$data = [])
    {
        $form = $this->form;

        $form->hidden('id');

        $form->text('name', '设备名称')->required();
        $form->select('device_type', '设备类型')->dataUrl(url("/admin/equipmenttype/selectPage"))->required();

        if ($isEdit) {
            $form->show('create_time', '安装时间')->default('---');
            $form->show('update_time', '修改时间')->default('---');
        }
    }

    /**
     * 保存数据
     *
     * @param integer $id
     * @return void
     */
    private function save($id = 0)
    {
        $data = request()->post();

        $result = $this->validate($data, []);

        if (!$result) {
            $this->error($result);
        }

        if ($id) {
            // 编辑
            $data['update_time'] = date_now();
        } else {
            // 新增
            $data['status'] = 0; //设备入库，状态默认为待使用
            $data['create_time'] = date_now();
            $data['update_time'] = date_now();
        }

        return $this->doSave($data, $id);
    }
}